package com.rchat.pocmini.activity.binddevice;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.fragment.GroupsAdapter;

import java.util.List;

/**
 * Created by sudf on 2017/9/15.
 */

public class BindDeviceAdapter extends RecyclerView.Adapter<BindDeviceAdapter.BindDeviceHolder> {
    private List<String> mDevicesMac;
    private ItemTouchHelperAdapterCallback mItemTouchHelperAdapterCallback;

    public interface ItemTouchHelperAdapterCallback {

        /**
         * Item点击事件
         *
         * @param view
         * @param deviceMac
         */
        void onItemClick(View view, String deviceMac);
    }

    public BindDeviceAdapter(List<String> devicesMac, ItemTouchHelperAdapterCallback callback) {
        this.mDevicesMac = devicesMac;
        this.mItemTouchHelperAdapterCallback = callback;
    }

    public void setData(List<String> devicesMac) {
        this.mDevicesMac = devicesMac;
        notifyDataSetChanged();
    }

    @Override
    public BindDeviceAdapter.BindDeviceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_binddevice, parent, false);
        return new BindDeviceHolder(view);
    }

    @Override
    public void onBindViewHolder(BindDeviceHolder holder, final int position) {
        holder.bluetoothMac.setText(mDevicesMac.get(position));
        holder.unbindDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemTouchHelperAdapterCallback.onItemClick(v, mDevicesMac.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDevicesMac.size();
    }

    class BindDeviceHolder extends RecyclerView.ViewHolder {
        /**
         * 群组Item的Icon
         */
        private TextView bluetoothMac;
        private View unbindDevice;

        public BindDeviceHolder(View itemView) {
            super(itemView);
            bluetoothMac = (TextView) itemView.findViewById(R.id.tv_device_mac);
            unbindDevice = itemView.findViewById(R.id.btn_unbinddevice);
        }
    }
}
